/**   
 * @Title: SettingAct.java 
 * @Package com.esun.cheetah.view.setting
 * @Description: 设置
 * @author Kuroky Chengxt@500wan.com   
 * @date 2012-03-13 下午05:07:33  
 * Copyright (C) 2011 Shenzhen E-sun Sky Net Technology Co.,ltd.
 */
package com.esun.cheetah.view.setting;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.esun.cheetah.R;
import com.esun.cheetah.biz.personal.attention.LoginOutComm;
import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.biz.setting.CheckUpgradeCommunicater;
import com.esun.cheetah.biz.setting.CheckUpgradeCommunicater.UpgradeInfo;
import com.esun.cheetah.config.Config;
import com.esun.cheetah.config.Language;
import com.esun.cheetah.view.custom.UpgradeDialog;
import com.esun.cheetah.view.setting.service.NotificationService;
import com.esun.util.VersionUtils;

/**
 * 设置 com.esun.cheetah.view.setting.SettingAct
 * 
 * @author 任付江 create at 2012-3-30 上午10:40:53
 */
public class SettingAct extends Activity implements OnClickListener {
	/** back键返回码 */
	private static final int SEND_SMS_REQUEST = 100;
	/** 未登录 */
	private RelativeLayout rlyAccountProctecNoLogin;
	/** 已经登录 */
	private RelativeLayout rlyAccountProctec;
	/** 定制比赛信息RelativeLayout */
	private RelativeLayout rlyCustomGameInfo;
	/** 定制比赛提醒时间RelativeLayout */
	private RelativeLayout rlyCustomMachTime;
	/** 用户建议RelativeLayout */
	private RelativeLayout rlyUserSuggest;
	/** 帮助RelativeLayout */
	private RelativeLayout rlyHelper;
	/** 关于RelativeLayout */
	private RelativeLayout rlyAbout;
	/** 退出RelativeLayout */
	private RelativeLayout rlyExit;
	/** 接收通讯成功消息 */
	Handler setHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:
				break;
			}
		}
	};
	private RelativeLayout rlyCheckUpgrade;

	/** 注销handler */
	Handler loginOutHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Config.COMMUNICATER_SUCCESS:

				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting);
		inintViews();
	}

	private void inintViews() {
		rlyAccountProctecNoLogin = (RelativeLayout) findViewById(R.id.rlyAccountProctecNoLogin);
		rlyAccountProctecNoLogin.setOnClickListener(this);
		rlyAccountProctec = (RelativeLayout) findViewById(R.id.rlyAccountProctec);
		rlyAccountProctec.setOnClickListener(this);
		rlyCustomGameInfo = (RelativeLayout) findViewById(R.id.rlyCustomGameInfo);
		rlyCustomGameInfo.setOnClickListener(this);
		if (UserInfo.getUserInfo().getCk().equals("")) {
			rlyAccountProctec.setVisibility(View.GONE);
			rlyAccountProctecNoLogin.setVisibility(View.VISIBLE);
		}
		rlyCustomMachTime = (RelativeLayout) findViewById(R.id.rlyCustomMachTime);
		rlyCustomMachTime.setOnClickListener(this);
		rlyUserSuggest = (RelativeLayout) findViewById(R.id.rlyUserSuggest);
		rlyUserSuggest.setOnClickListener(this);
		rlyHelper = (RelativeLayout) findViewById(R.id.rlyHelper);
		rlyHelper.setOnClickListener(this);
		rlyAbout = (RelativeLayout) findViewById(R.id.rlyAbout);
		rlyAbout.setOnClickListener(this);
		rlyExit = (RelativeLayout) findViewById(R.id.rlyExt);
		rlyExit.setOnClickListener(this);
		rlyCheckUpgrade = (RelativeLayout) findViewById(R.id.rly_check_upgrade);
		rlyCheckUpgrade.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		int viewId = v.getId();
		switch (viewId) {
		case R.id.rlyAccountProctecNoLogin:
			Toast.makeText(this, "请登录", Toast.LENGTH_SHORT).show();
			break;
		case R.id.rlyAccountProctec:
			Intent ctIntent = new Intent(this, ChooseProTime.class);
			startActivity(ctIntent);
			break;
		case R.id.rlyCustomGameInfo:
			Intent setIntent = new Intent(this, SetDetailsAct.class);
			startActivity(setIntent);
			break;
		case R.id.rlyCustomMachTime:
			Intent timeIntent = new Intent(this, TimeAct.class);
			startActivityForResult(timeIntent, SEND_SMS_REQUEST);
			break;
		case R.id.rlyUserSuggest:
			Intent sugIntent = new Intent(this, UserSuggestAct.class);
			startActivity(sugIntent);
			break;
		case R.id.rlyHelper:
			Intent helpIntent = new Intent(this, HelpAct.class);
			startActivity(helpIntent);
			break;
		case R.id.rlyAbout:
			Intent aboutIntent = new Intent(this, AboutAct.class);
			startActivity(aboutIntent);
			break;
		case R.id.rlyExt:
			exitDialog();
			break;
		case R.id.rly_check_upgrade:
			checkUpgrade();
			break;
		default:

		}
	}

	/**
	 * @title checkUpgrade
	 * @description 检查更新
	 */
	private void checkUpgrade() {
		Toast.makeText(this, "正在检测新版本...", Toast.LENGTH_LONG).show();
		Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				if (msg.what == Config.COMMUNICATER_SUCCESS) {
					VersionUtils.getInstance().setActived(SettingAct.this);
					UpgradeInfo info = (UpgradeInfo) msg.obj;
					if (info.isUpgrade) {
						showUpgradeDialog(info);
					} else {
						showNotNeedUpgrade();
					}
				} else {
					Toast.makeText(SettingAct.this.getParent(), "检测失败！",
							Toast.LENGTH_SHORT).show();
				}
			}

		};
		final String channel = VersionUtils.getInstance().getChannel(this);
		final int version = VersionUtils.getInstance().getVersion(this);
		final int isJiHuo = VersionUtils.getInstance().getIsActive(this);
		final String sysVersion = Build.VERSION.RELEASE;
		final String screenreso = VersionUtils.getInstance().getScreenreso(this);
		final String serial = VersionUtils.getInstance().getSerial(this);
		if (TextUtils.isEmpty(channel) || version < 0) {
			return;
		}
		CheckUpgradeCommunicater mCheckUpgradeCommunicater = new CheckUpgradeCommunicater(handler, this,version, channel, sysVersion, screenreso, serial, isJiHuo);
		mCheckUpgradeCommunicater.startCommunicate();
		

	}

	/**
	 * @title showNotNeedUpgrade
	 * @description
	 */
	protected void showNotNeedUpgrade() {
		Toast.makeText(this, "当前已是最新版本！", Toast.LENGTH_SHORT).show();

	}

	/**
	 * @title showUpgradDialog
	 * @description 显示下载的对话框
	 * @param info
	 */
	protected void showUpgradeDialog(final UpgradeInfo info) {
		final UpgradeDialog dialog = new UpgradeDialog(this);
		dialog.setMessage(Html.fromHtml(info.upgradeDescription));
		dialog.setOnLeftButtonClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setOnRightButtonClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent downloadIntent = new Intent(Intent.ACTION_VIEW, Uri
						.parse(info.url));
				startActivity(downloadIntent);
			}
		});
		dialog.show();
		
		
		
	}

	/**
	 * 退出对话框
	 */
	public void creatDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(getParent());
		builder.setMessage(Language.LEAVE);
		builder.setPositiveButton(Language.SURE,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {

						new LoginOutComm(loginOutHandler).startCommunicate();
						Intent i = new Intent();
						i.setClass(SettingAct.this, NotificationService.class);
						SettingAct.this.stopService(i);
						dialog.cancel();
						finish();
					}
				});
		builder.setNegativeButton(Language.CANCEL,
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
		builder.create().show();
	}
	@Override
	protected void onResume() {
		super.onResume();
	}

	/**
	 * 退出对话框
	 */
	public void exitDialog() {
		final Dialog inputPwdDialog = new Dialog(SettingAct.this,
				R.style.dialog);
		inputPwdDialog.getWindow().setContentView(R.layout.dialog_quit);
		Button btSure = (Button) inputPwdDialog.getWindow().findViewById(
				R.id.btSure);
		Button btCancel = (Button) inputPwdDialog.getWindow().findViewById(
				R.id.btCancel);
		btSure.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new LoginOutComm(loginOutHandler).startCommunicate();
				inputPwdDialog.cancel();
				Intent i = new Intent();
				i.setClass(SettingAct.this, NotificationService.class);
				SettingAct.this.stopService(i);
				finish();
				android.os.Process.killProcess(android.os.Process.myPid());
			}
		});
		btCancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				inputPwdDialog.cancel();
			}
		});
		inputPwdDialog.show();
	}
}
